package com.mairuide.datalog;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.base.Constant;
import com.mairuide._frame.base.ModuleCode;
import com.mairuide._frame.base.PageSupport;
import com.mairuide._frame.utils.StringUtils;
import com.mairuide.bill.DealFlow;
import com.mairuide.bill.DealFlowService;
import com.mairuide.entity.PageResult;
import com.mairuide.phone.import_phone.PhoneRecordingService;

@Controller
@RequestMapping("/datalog")
public class DataOperationLogController extends BaseController {

	@Resource
	private DataOperationLogService dataOperationLogService;
	
	@Resource
	private DataOperationDetailLogService dataOperationDetailLogService;
	
	@Resource
	private DataOperationImportLogService dataOperationImportLogService;
	
	@Resource
	private DealFlowService dealFlowService;
	@Resource
	private PhoneRecordingService phoneRecordingService;
	
	/**
	 * 日志列表
	 * @param name
	 * @param gwName
	 * @param model
	 * @param pagesupport
	 * @return
	 */
	@ModuleCode(pValue="operations", value="datalog")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Object list(DataOperationLogParams paramsDataLog, ModelMap model, PageSupport pagesupport) {
		Sort sort = new Sort(Direction.DESC, "logId");
		/* 查询运营管理平台日志信息 */
		paramsDataLog.setSystemFlag(SystemFlag.ZD.ordinal());
		paramsDataLog.setShowType(1);
		/* 分页查询日志数据 */ 
		Page<DataOperationLog> page = dataOperationLogService.findDataOperationLoglList(paramsDataLog, pagesupport.getPage(sort));
		/* 封装模块名称 */
		Map<String,String> moduleFlagMap = new HashMap<String,String>();
		if(page.getContent() != null && !page.getContent().isEmpty()) {
			for(DataOperationLog dol : page.getContent()) {
				moduleFlagMap.put(String.valueOf(dol.getLogId()), dol.getModuleFlag().getName());
			}
		}
		model.addAttribute("page", page);
		model.addAttribute("actionTypes", ActionType.values());
		model.addAttribute("moduleFlags", ModuleFlag.values());
		model.addAttribute("moduleFlagMap", moduleFlagMap);
		model.addAttribute("paramsDataLog", paramsDataLog);
		return "/datalog/list";
	}

	@RequestMapping(value = "/importlist", method = RequestMethod.GET)
	public Object importLists(ModelMap model){
		return "/datalog/importList";
	}
	/**
	 * 导入日志列表
	 * @param
	 * @param
	 * @return
	 */
	//@ModuleCode(pValue="billexport", value="datalog_inport")
	@RequestMapping(value = "/importDataList", method = RequestMethod.POST)
	@ResponseBody
	public Object importlist(HttpServletRequest request, Integer pageNumber, Integer pageSize) {
		String uid = request.getSession().getAttribute("caseId") == null?"":request.getSession().getAttribute("caseId").toString();
		return dataOperationImportLogService.findByDealFlowUuidPage(uid,pageNumber,pageSize,null);
	}
	@RequestMapping(value = "/importDataList_easyui", method = RequestMethod.POST)
	@ResponseBody
	public PageResult importlist_easyui(Integer page, Integer rows, String ids,String caseId) {
		PageResult pageResult = dataOperationImportLogService.findByDealFlowUuidPage(caseId, page-1, rows, ids);
		return pageResult;
	}
	/**
	 * 日志详情查看
	 * @param model
	 * @param parentLogId
	 * @return
	 */
	@RequestMapping(value="/dataDetailLog", method=RequestMethod.GET)
	public Object dataDetailLog(ModelMap model, Integer parentLogId) {
		/* 查询日志父表信息   */
		DataOperationLog dol = dataOperationLogService.findOne(parentLogId);
		/* 封装模块名称 */
		Map<String,String> moduleFlagMap = new HashMap<String,String>();
		moduleFlagMap.put(String.valueOf(dol.getLogId()), dol.getModuleFlag().getName());
		/* 查询日志子表信息   */
		List<DataOperationDetailLog> dodls = dataOperationDetailLogService.findByParentLogIdOrderByDetailId(parentLogId);
		model.addAttribute("u", dol);
		model.addAttribute("moduleFlagMap", moduleFlagMap);
		model.addAttribute("dataOperationDetailLog", dodls);
		return "/datalog/detail";
	}
	
	/**
	 * 导入日志详情查看
	 * @param model
	 * @param parentLogId
	 * @return
	 */
	@RequestMapping(value="/dataImportLog", method=RequestMethod.GET)
	public Object dataImportLog(ModelMap model, Integer parentLogId) {
		/* 查询日志父表信息   */
		DataOperationLog dol = dataOperationLogService.findOne(parentLogId);
		/* 封装模块名称 */
		Map<String,String> moduleFlagMap = new HashMap<String,String>();
		moduleFlagMap.put(String.valueOf(dol.getLogId()), dol.getModuleFlag().getName());
		/* 查询日志子表信息   */
		List<DataOperationImportLog> dodls = dataOperationImportLogService.findByParentLogIdOrderByImportDetailId(parentLogId);
		model.addAttribute("u", dol);
		model.addAttribute("moduleFlagMap", moduleFlagMap);
		model.addAttribute("dataOperationImportLog", dodls);
		return "/datalog/importDetail";
	}
	
	/**
	 * 根据uuid删除本次导入流水信息
	 * @param dolp
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteFlow")
	public Object deleteFlow(DataOperationLogParams dolp) {
		if (StringUtils.isEmpty(dolp.getDealFlowUuid())) {
			return ERROR("uuid is empty");
		}
		if (dolp.getTableFlat()==0){

		}else if (dolp.getTableFlat()==1) {
			dealFlowService.deleteFlow4Uuid(dolp.getCaseManagerId(), dolp.getDealFlowUuid());
		}else if (dolp.getTableFlat()==2){
			phoneRecordingService.deleteByUuid(dolp.getCaseManagerId(), dolp.getDealFlowUuid());
		}
		DataOperationImportLog dol = dataOperationImportLogService.findOne(dolp.getImportDetailId());
		dol.setIsDelete(1);
		dataOperationImportLogService.save(dol);
		
		dealFlowService.clearCache1(dolp.getCaseManagerId());
		dealFlowService.clearCache2(dolp.getCaseManagerId());
		dealFlowService.clearCache3(dolp.getCaseManagerId());
		dealFlowService.clearCache4(dolp.getCaseManagerId());
		dealFlowService.clearCache5(dolp.getCaseManagerId());
		return SUCCESS();
	}
	/**
	 * 标记日志为已处理
	 * @param model
	 * @param beanId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/dealWith")
	public Object dealWith(ModelMap model, @RequestParam("logId") Integer logId) {
		if (StringUtils.isEmpty(logId)) {
			return ERROR("id is empty");
		}
		/* 查询日志父表信息   */
		DataOperationLog dol = dataOperationLogService.findOne(logId);
		dol.setDealWith(1);//标记为已处理
		dataOperationLogService.save(dol);
		return SUCCESS();
	}
	
	/**
	 * 标记日志为已处理
	 * @param model
	 * @param beanId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/restore")
	public Object restore(ModelMap model, @RequestParam("beanIds") String beanIds,String oldValue,String actionType) {
		if (StringUtils.isEmpty(beanIds)) {
			return ERROR("beanId is empty");
		}
		beanIds = beanIds.replaceAll(" ", ""); 
		String caseId = request.getSession().getAttribute("caseId") == null?"":request.getSession().getAttribute("caseId").toString();
		List<String> accountBodyCards = Arrays.asList(beanIds.split(","));
		List<DealFlow> dfs = dealFlowService.findByAccountBodyCardAndCasemanageId(accountBodyCards, caseId);
		if(null != dfs && dfs.size() > 0) {
			for(DealFlow df : dfs) {
				if(actionType.equals("删除")) {
					df.setIsDelete(Constant.NOT_DELETED);
				}else if(actionType.equals("修改")) {
					df.setAccountBody(oldValue);
				}
			}
			dealFlowService.save(dfs);
		}
		return SUCCESS();
	}
	
}
